package cn.net.zidian.mvvm.activity;

import android.view.LayoutInflater;
import android.view.View;

import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;

import cn.net.zidian.bean.HistoryBean;
import cn.net.zidian.bean.IPBean;
import cn.net.zidian.bean.IdCardBean;
import cn.net.zidian.databinding.ActivityIdcardSearchBinding;
import cn.net.zidian.databinding.ActivityIpSearchBinding;
import cn.net.zidian.mvvm.vm.IPVM;
import cn.net.zidian.mvvm.vm.IdCardVM;
import cn.net.zidian.util.HistoryData;

public class IdCardActivity extends BaseActivity<IdCardVM> {

    private ActivityIdcardSearchBinding viewBinding;

    @Override
    protected IdCardVM viewModel() {
        return new IdCardVM();
    }

    @Override
    protected void initView() {
        viewBinding = ActivityIdcardSearchBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        viewBinding.titleBar.getLeftView().setOnClickListener(v -> finish());
        initObserver();
        viewBinding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String idCard = viewBinding.etSearch.getText().toString();
                if (RegexUtils.isIDCard18(idCard)) {
                    HistoryData.list.add(new HistoryBean(idCard, "身份证号查询"));
                    vm.search(idCard);
                } else {
                    ToastUtils.showShort("请输入身份证号");
                }
            }
        });
    }

    private void initObserver() {
        vm.event.observe(this, new Observer<IdCardBean>() {
            @Override
            public void onChanged(IdCardBean ipBean) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("省份:").append(ipBean.getProvince())
                        .append("\n").append("城市:").append(ipBean.getCity())
                        .append("\n").append("区:").append(ipBean.getTown())
                        .append("\n").append("性别:").append(ipBean.getSex())
                        .append("\n").append("出生年月日:").append(ipBean.getBirth());
                viewBinding.tvContent.setText(stringBuilder);
            }
        });

    }


}
